require '../lib/ratesdk.rb'

client = RateClient.new(host: 'rate.pku.edu.cn', port: 2015, email: 'xianran@pku.edu.cn', password: '12345')

puts "Login [OK]"

views = client.list(target: 'view')
views.contents.each do |v|
  client.delete(target: 'view', uuid: v['uuid']) if v['name'] == 'myview'
end

def add(a, b)
	return a+b
end

def add(&block)
	yield
end

view = client.create(target: 'view', 
                     name: 'myview', 
                     import_tag: '2011summer', 
                     strategy: 'import_tag',
                     desc: 'testview')

puts "Create view by import_tag [OK]"

result = client.delete(target: 'view', uuid: view.uuid)

puts "Delete view [OK]"

view = client.create(target: 'view',
                     name: 'myview2',
                     strategy: 'file',
                     path: 'uuids.txt',
                     desc: 'testview')
# client.delete(target: 'view', uuid: view.uuid)
puts "Create view by file [OK]"
begin
  benchmark = client.create(target: 'benchmark',
                          name: 'mybench',
                          strategy: 'general',
                          desc: 'no',
                          view: view,
                          class_count: 3,
                          sample_count: 1
                          )
  puts "Create general benchmark [OK]"
  client.delete(target: 'benchmark', uuid: benchmark.uuid)

  benchmark = client.create(target: 'benchmark',
                          name: 'mybench',
                          strategy: 'allinner',
                          desc: 'no',
                          view: view,
                          )
  puts "Create allinner benchmark [OK]"
  client.delete(target: 'benchmark', uuid: benchmark.uuid)

  benchmark = client.create(target: 'benchmark',
                          name: 'mybench',
                          strategy: 'file',
                          desc: 'no',
                          view: view,
                          path: 'benchmark.txt'
                          )
  puts "Create file benchmark [OK]"
  client.download(target: 'benchmark', uuid: benchmark.uuid, path: 'benchmark.zip')
  client.delete(target: 'benchmark', uuid: benchmark.uuid)
  `rm benchmark.zip`

  algorithm = client.create(target: 'algorithm', name: 'myalg', path: 'alg.zip', tag: 'tag1')
  puts "Create algoritm [OK]"

  benchmark = client.create(target: 'benchmark',
                          name: 'mybench',
                          strategy: 'allinner',
                          desc: 'no',
                          view: view,
                          )

  task = client.run(auuid: algorithm.uuid, buuid: benchmark.uuid)
  puts "Run task [OK]"
  loop do
    info = client.info(target: 'task', uuid: task.uuid)
    break if info.done == 1
    puts info
    sleep 1
  end
  puts "Task finished"
ensure
  client.delete(target: 'view', uuid: view.uuid)
  client.delete(target: 'algorithm', uuid: algorithm.uuid)
  client.delete(target: 'benchmark', uuid: benchmark.uuid)
end

client.destroy
